﻿using ShanNiang.Service.Constants;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ShanNiang.Packing
{
    static class WinApis
    {
        #region winApi 扩展
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hFile"></param>
        /// <param name="lpAttributes"></param>
        /// <param name="flProtect"></param>
        /// <param name="dwMaximumSizeHigh"></param>
        /// <param name="dwMaximumSizeLow"></param>
        /// <param name="lpName"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpAttributes, uint flProtect, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);
        /// <summary>
        /// 查找窗体
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("user32")]
        internal static extern IntPtr FindWindow(string? lpClassName, string lpWindowName);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hFileMappingObject"></param>
        /// <param name="dwDesiredAccess"></param>
        /// <param name="dwFileOffsetHigh"></param>
        /// <param name="dwFileOffsetLow"></param>
        /// <param name="dwNumberOfBytesToMap"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);
        /// <summary>
        /// 设置到前端显示
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32")]
        internal static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns></returns>
        [DllImport("user32")]
        internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        #endregion


        /// <summary>
        /// 获取存在的window 句柄
        /// </summary>
        /// <returns></returns>
        internal static IntPtr GetExistingWindowHandle()
        {
            IntPtr hMap = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, 8, CommonData.SharedMemoryName);
            if (hMap == IntPtr.Zero) return IntPtr.Zero;

            IntPtr pBuf = MapViewOfFile(hMap, 0xF001F, 0, 0, 8);
            return Marshal.ReadIntPtr(pBuf);

        }
        /// <summary>
        /// 保存window 句柄
        /// </summary>
        /// <param name="hWnd"></param>
        internal static void SaveWindowHandle(IntPtr hWnd)
        {
            // 创建共享内存保存句柄
            IntPtr hMap = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, 8, CommonData.SharedMemoryName);
            IntPtr pBuf = MapViewOfFile(hMap, 0xF001F, 0, 0, 8);
            Marshal.WriteIntPtr(pBuf, hWnd);
        }
    }
}
